<?php

namespace App\Admin\Controllers\Goods;

use App\Models\Goods;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;


class RecycleController extends Controller
{
    use ModelForm;
    protected $header = '商品回收站';

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header($this->header);
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Goods::class, function (Grid $grid) {
            $grid->model()->onlyTrashed();
            $grid->id('编号')->sortable();
            $grid->goods_img('图片')->image('','50','50');
            $grid->goods_name('商品名称');
            $grid->goods_price('价格');
            $grid->created_at('创建时间');
            $grid->deleted_at('删除时间');

            //禁用创建按钮
            $grid->disableCreateButton();

            //搜索栏
            $grid->filter(function (Grid\Filter $filter) {
                $filter->like('goods_name','商品名称');
                $filter->between('created_at', '创建时间')->datetime();
            });

            //操作列
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                //禁用查看
                $actions->disableView();
                $actions->disableEdit();
                //添加恢复按钮
                $actions->addRestore();
            });
        });
    }


    /**
     * 恢复
     *
     * @param $id
     */
    public function restore($id)
    {
        $flag = $this->form()->model()->withTrashed()->find($id)->restore();

        if ($flag){
            admin_toastr('恢复成功', 'success');
        } else {
            admin_toastr('恢复失败', 'error');
        }
    }

    /**
     * 彻底删除
     *
     * @param $id
     */
    public function destroy($id)
    {
        $flag = $this->form()->model()->withTrashed()->find($id)->forceDelete();

        if ($flag){
            admin_toastr('删除成功', 'success');
        } else {
            admin_toastr('删除失败', 'error');
        }
    }




}
